const fs = require('fs');
const { resolveApp } = require('./paths');

const theme = {};
const root = {};

fs.readFileSync(resolveApp('src/styles/index.scss')).toString().split('\n').forEach(line => {
	line = line.trim();
	if (line && line.startsWith('--')) {
		const s = line.split(/[;: $]/);
		root[s[0]] = s[2];
	}
});

fs.readFileSync(resolveApp('src/styles/var.scss')).toString().split(/\n/).forEach(v => {
	if (v) {
		const target = v.split(':');
		let value = target[1].replace(/;/, '').trim();
		if (value.startsWith('var')) {
			const match = value.match(/var\(([\s\S]+)\)/);
			value = root[match[1]];
		}
		theme[target[0].replace(/\$/, '')] = value;
	}
});

module.exports = {
	...theme,
};
